package com.dxx.cloud.admin.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PinYinUtils {

    /**
     * 将汉字转成拼音
     * <P>
     * 取首字母或全拼
     *
     * @param hanzi 汉字字符串
     * @param isFull 是否全拼 true:表示全拼 false表示：首字母
     *
     * @return 拼音
     */
    public static String convertHanzi2Pinyin(String hanzi,boolean isFull){
        /***
         * ^[\u2E80-\u9FFF]+$ 匹配所有东亚区的语言
         * ^[\u4E00-\u9FFF]+$ 匹配简体和繁体
         * ^[\u4E00-\u9FA5]+$ 匹配简体
         */
        List<String> list = convertHanzi2Pinyin(hanzi);
        StringBuffer sb = new StringBuffer();
        for (String s : list) {
            if(isFull){
                sb.append(s);
            }else {
                sb.append(s.charAt(0));
            }
        }
        return sb.toString();
    }

    /**
     * 全字母和首字母
     * @param hanzi
     * @return
     */
    public static String[] convertHanzi2PinyinWithFullAndFirst(String hanzi){
        List<String> list = convertHanzi2Pinyin(hanzi);
        StringBuffer fullName = new StringBuffer();
        StringBuffer firstLetter = new StringBuffer();
        for (String s : list) {
            fullName.append(s);
            firstLetter.append(s.charAt(0));
        }
        return new String[]{fullName.toString(),firstLetter.toString()};
    }

    public static List<String> convertHanzi2Pinyin(String hanzi) {
        List<String> list = new ArrayList<>();
        /***
         * ^[\u2E80-\u9FFF]+$ 匹配所有东亚区的语言
         * ^[\u4E00-\u9FFF]+$ 匹配简体和繁体
         * ^[\u4E00-\u9FA5]+$ 匹配简体
         */
        String regExp="^[\u4E00-\u9FFF]+$";
        if(hanzi==null||"".equals(hanzi.trim())){
            return list;
        }
        for(int i=0;i<hanzi.length();i++){
            char unit=hanzi.charAt(i);
            //是汉字，则转拼音
            if(match(String.valueOf(unit),regExp)){
                list.add(convertSingleHanzi2Pinyin(unit));
            }else{
                list.add(unit + "");
            }
        }
        return list;
    }

    /**
     * 将单个汉字转成拼音
     *
     * @param hanzi 汉字字符
     *
     * @return 拼音
     */
    private static String convertSingleHanzi2Pinyin(char hanzi){
        HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
        outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        String[] res;
        StringBuffer sb=new StringBuffer();
        try {
            res = PinyinHelper.toHanyuPinyinStringArray(hanzi,outputFormat);
            sb.append(res[0]);//对于多音字，只用第一个拼音
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return sb.toString();
    }

    /***
     * 匹配
     * <P>
     * 根据字符和正则表达式进行匹配
     *
     * @param str 源字符串
     * @param regex 正则表达式
     *
     * @return true：匹配成功  false：匹配失败
     */
    private static boolean match(String str,String regex){
        Pattern pattern=Pattern.compile(regex);
        Matcher matcher=pattern.matcher(str);
        return matcher.find();
    }

    public static void main(String[] args) {
//        String hanzi = "老铁6666asdf我在哪里";
//        String pinyin = convertHanzi2Pinyin(hanzi,false);
//        System.out.println(pinyin);
        String namesStr = "";
        for (String name : namesStr.split("\n")) {
            System.out.println(convertHanzi2PinyinWithFullAndFirst(name)[0]);
        }
//        for (String s : convertHanzi2PinyinWithFullAndFirst(hanzi)) {
//            System.out.println(s);
//        }
    }
}
